package com.example.volleyball.Tools;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class DBTools {

    SQLiteDatabase helper;

    /**
     *
     * @param db
     */
    public DBTools(DataDB db){
        /**
         * db.getWritableDatabase();
         * 创建和/或打开将用于读取和写入的数据库。 第一次调用时，将打开数据库并onOpen onCreate 、 onUpgrade和/或onOpen
         * return : 在调用close之前有效的读/写数据库对象
         */
        this.helper = db.getWritableDatabase();
    }

    //插入
    public void insert(String account,String time,String field,String hour,String name){
        ContentValues values = new ContentValues();
        values.put("account",account);
        values.put("time",time);
        values.put("field",field);
        values.put("hour",hour);
        values.put("name",name);
        helper.insert("Data",null,values);
    }

    //查询
    public List<Tools> select(){
        List<Tools> list = new ArrayList<>();
        Cursor cursor = helper.query("Data",null,null,null,null,null,null);
        if (cursor.moveToFirst()){
            do {
                String time = cursor.getString(cursor.getColumnIndex("time"));
                String field = cursor.getString(cursor.getColumnIndex("field"));
                String hour = cursor.getString(cursor.getColumnIndex("hour"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                list.add(new Tools(time,field,hour,name));
            }while (cursor.moveToNext());
        }
        return list;
    }
}
